package com.sg.business.commons.ui.work;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;

import com.mobnut.db.model.IContext;
import com.mobnut.db.model.PrimaryObject;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
import com.sg.widgets.part.editor.page.AbstractFormPageDelegator;
import com.sg.widgets.registry.config.BasicPageConfigurator;

public abstract class AbstractProjectChangePage extends
		AbstractFormPageDelegator {

	protected IContext context;
	private ScrolledComposite sc;
	private ProjectChangePanel pcp;

	@Override
	public Composite createPageContent(IManagedForm mForm, Composite parent,
			PrimaryObjectEditorInput input, BasicPageConfigurator conf) {

		parent.setLayout(new FillLayout());

		context = new CurrentAccountContext();
		setFormInput(input);

		sc = new ScrolledComposite(parent, SWT.V_SCROLL);
		pcp = new ProjectChangePanel(sc, SWT.NONE) {
			@Override
			protected IContext getContext() {
				return context;
			}
		};
		PrimaryObject data = input.getData();
		pcp.setInput(data);
		pcp.setEditable(getEditable());
		pcp.createContent();

		sc.setContent(pcp);

		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);
		sc.setAlwaysShowScrollBars(true);
		sc.setMinSize(pcp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		sc.addControlListener(new ControlAdapter() {

			@Override
			public void controlResized(ControlEvent e) {
				sc.setMinSize(pcp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
			}
		});
		return sc;
	}

	protected abstract boolean getEditable();

	@Override
	public void commit(boolean onSave) {
		setDirty(false);
	}

	@Override
	public void setFocus() {
	}

	@Override
	public boolean createBody() {
		return false;
	}

}
